package cn.com.smart.module.tool.fileList.controller;

import cn.com.smart.common.utils.file.FileUploadUtils;
import cn.com.smart.framework.config.SysConfig;
import cn.com.smart.framework.web.domain.AjaxResult;
import cn.com.smart.module.tool.fileList.domain.fileList;
import cn.com.smart.module.tool.fileList.service.fileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileReader;
import java.util.List;

import static cn.com.smart.framework.web.domain.AjaxResult.error;

@Controller
@RequestMapping("/tool/file")
public class fileController {
	private String prefix = "tool/file";
    @Autowired
    private fileService fileService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/uplodeFile",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult uplodeFile(@RequestParam("file_data") MultipartFile file){

        try {
            if (!file.isEmpty()) {
                String Filename=file.getOriginalFilename();
                String tailName=Filename.substring(Filename.indexOf("."));

                /*添加记录*/
                fileList fileList=new fileList();
                fileList.setFileName(Filename);

                if (tailName.equals(".gif") || tailName.equals(".jpeg") || tailName.equals(".png ") || tailName.equals(".svg") || tailName.equals(".jpg")){
                    String avatar = FileUploadUtils.upload(SysConfig.getAvatarPath(), file, tailName);
                    fileList.setRoute(avatar);
                    fileList.setFiletype(0);
                    fileService.addFileList(fileList);

                    return AjaxResult.success(avatar);
                }else {

                    String avatar = FileUploadUtils.upload(SysConfig.getAvatarPath(), file, tailName);
                    fileList.setRoute(avatar);
                    fileList.setFiletype(1);
                    fileService.addFileList(fileList);

                    return AjaxResult.success(avatar);
                }
            }
            return error("没有文件！");
        }catch (Exception e){
            System.out.println("上传文件失败！");
            return error(e.getMessage());
        }
    }
    /**
     * 文件上传预览
     */
    @GetMapping("/commit")
    public String commit()
    {
        return prefix + "/commit";
    }
    /**
     * 文件上传预览
     */
    @GetMapping("/avatar")
    public String avatar()
    {
        return prefix + "/avatar";
    }
    /**
     * 图片预览
     */
    @GetMapping("/openImg")
    public String openImg()
    {
        return prefix + "/openImg";
    }
    /**
     * 获取本地文件列表
     * @return
     */
    @RequestMapping("/getFileList")
    @ResponseBody
    public AjaxResult getFileList(){

        /*获取文件列表地址*/
        List<fileList> fileList=fileService.getFileList();

        for (fileList file : fileList){

            try {
//               file.setFileInfor(getFile(SysConfig.getAvatarPath()+file.getRoute()));
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return AjaxResult.success(fileList);
    }

    /**
     * 删除文件内容
     * @param ids
     * @return
     */
    @RequestMapping("/deleteFile")
    @ResponseBody
    public AjaxResult deleteFile(String ids){
        int a=fileService.deleteFile(ids);
        return AjaxResult.success(a);
    }

    /**
     * 获取路径下的文件
     * @param path
     * @return
     */
    private String getFile(String path){
        //创建文件对象
        File file=new File(path);

        /*文件对象是否存在*/
        if (!file.exists()){
            throw new RuntimeException();
        }

        //字符流的读出数据
        try {
            FileReader fileReader=new FileReader(path);
            //缓冲区
            char [] deopsit=new char[100];
            int count=0;

            /*写入数据*/
            while (fileReader.read(deopsit) != -1){
                String infor=new String(deopsit,0,deopsit.length);
                System.out.println(infor);
                fileReader.close();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
